import GameView from "../../../../quick/core/ui/GameView";
import { inject } from "../../../../quick/defines/Decorators";
import { ButtonSpriteType } from "../../../../quick/defines/Enums";
import { Macro } from "../../../../quick/defines/Macros";


const { ccclass, property } = cc._decorator;

@ccclass
export default class LoadTestView extends GameView {

    public static getPrefabUrl() {
        return "prefabs/LoadTestView";
    }

    @inject("loadButton", cc.Node)
    private loadButton: cc.Node = null;
    @inject("TableView", cc.Node)
    private tableView: cc.Node = null!;
    @inject("content", cc.Node)
    private content: cc.Node = null!;
    onLoad() {
        super.onLoad();
        this.onN(cc.find("goback", this.node), cc.Node.EventType.TOUCH_END, this.onGoback);

        let op = cc.find("op", this.node);
        this.onN(cc.find("loadFont", op), cc.Node.EventType.TOUCH_END, this.onLoadFont);

        this.onN(cc.find("loadImg", op), cc.Node.EventType.TOUCH_END, this.onLoadImg);

        this.onN(cc.find("loadNetImg", op), cc.Node.EventType.TOUCH_END, this.onLoadNetImg);

        this.onN(cc.find("loadButton", op), cc.Node.EventType.TOUCH_END, this.onLoadButton);

        this.onN(cc.find("loadParticle", op), cc.Node.EventType.TOUCH_END, this.onLoadParticle);

        this.onN(cc.find("loadSpine", op), cc.Node.EventType.TOUCH_END, this.onLoadSpine);
        this.onN(cc.find("loadNetSpine", op), cc.Node.EventType.TOUCH_END, this.onLoadNetSpine);

        this.onN(cc.find("loadDir", op), cc.Node.EventType.TOUCH_END, this.onLoadDir);

        this.onN(cc.find("loadDragon", op), cc.Node.EventType.TOUCH_END, this.onLoadDragon);

        this.onN(cc.find("tableView", op), cc.Node.EventType.TOUCH_END, this.onTableView);
        this.tableView.removeFromParent();
    }

    private onGoback() {
        this.enterBundle(Macro.BUNDLE_HALL);
    }

    private onLoadFont() {
        if (this.content.getChildByName("font")) {
            return;
        }
        this.content.removeAllChildren();
        let node = new cc.Node();
        node.name = "font";
        this.content.addChild(node);
        let label = node.addComponent(cc.Label);
        label.loadFont({
            //不提前加载目录方式
            font: "font/number",

            //提前加载目录资源方式
            // font : "number",
            // dir : "font",

            view: this,
            complete: (font) => {
                if (font) {
                    label.string = "+12345678.9万";
                }
            }, 
            bundle: this.bundle
        });
    }

    private onLoadImg() {
        let name = "testImg";
        if (this.content.getChildByName(name)) {
            return;
        }
        this.content.removeAllChildren();

        // createPrefab({
        //     //未提前以目录形式加载资源
        //     // url : "prefabs/Test",

        //     //提前以目录形式加载资源
        //     url : "Test",
        //     dir : "prefabs",

        //     view : this,
        //     complete : (node)=>{
        //         this.content.addChild(node);
        //         node.name = name;
        //     }
        // })
        // return;


        let node = new cc.Node();
        this.content.addChild(node);
        node.name = name;
        let sp = node.addComponent(cc.Sprite);

        // loadRes<cc.SpriteFrame>({
        //     view : this,
        //     // url : "texture/sheep/sheep_run_0",

        //     // 提前加载了目录
        //     url : "sheep_run_0",
        //     dir : "texture/sheep",
        //     type : cc.SpriteFrame,
        //     onComplete : (data)=>{
        //         sp.spriteFrame = data;
        //     }
        // })
        // return;

        sp.loadImage({

            //加载单个图片
            url:"texture/timg",

            //加载图集
            // url:{urls : ["common/images/lobby_texture"] , key : "update_status_hot"},
            // bundle : Macro.BUNDLE_RESOURCES,

            //提前以目录形式加载图集
            // url : { urls : ["lobby_texture"] , key : "update_status_hot"},
            // dir : "common/images",
            // bundle : Macro.BUNDLE_RESOURCES,

            //提前以目录形式加载图集
            // url: "setting_title",
            // dir: "common/texture",
            // bundle: Macro.BUNDLE_RESOURCES,

            view: this
        });

    }

    private onLoadNetImg() {
        let name = "netimg";
        if (this.content.getChildByName(name)) {
            return;
        }
        this.content.removeAllChildren();
        let node = new cc.Node();
        this.content.addChild(node);
        node.name = name;
        let sp = node.addComponent(cc.Sprite);
        sp.loadRemoteImage({
            // url : "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLeJcxcteU8DF5WwIicmKClkL6XQXY4uwAWBrBjaG5M171uqc3vibibWIdP4wp9ouibt1LyDp1zz4tBjA/132",
            // url : "https://p26.douyinpic.com/aweme/100x100/aweme-avatar/tos-cn-avt-0015_aa5859fa41538782706313a7294ad45c.jpeg?from=3782654143",
            url: "https://www.baidu.com/img/flexible/logo/pc/index_gray.png",
            view: this,
            defaultBundle: this.bundle,
            defaultSpriteFrame: "texture/timg"
        });
    }

    private onLoadButton() {
        let name = "button";
        if (this.content.getChildByName(name)) {
            return;
        }
        this.content.removeAllChildren();
        let button = cc.instantiate(this.loadButton);
        this.content.addChild(button);
        button.name = name;
        button.active = true;
        button.position = cc.v3(0, 0, 0);
        let btn = button.getComponent(cc.Button);
        btn.loadButton({
            
            //动态加载单图
            normalSprite: "texture/btn_b",
            pressedSprite: "texture/btn_y",
            hoverSprite: "texture/btnbg",
            bundle: Macro.BUNDLE_HALL,

            //加载图集
            // normalSprite:{urls : ["common/images/lobby_texture"] , key : "update_status_hot"},
            // pressedSprite:{urls : ["common/images/lobby_texture"] , key : "update_status_fav"},
            // hoverSprite:{urls : ["common/images/lobby_texture"] , key : "update_status_new"},
            // bundle : Macro.BUNDLE_RESOURCES,

            //提前以目录形式加载资源
            // normalSprite: "btn_b",
            // pressedSprite: "btn_y",
            // hoverSprite: "btnbg",
            // bundle: Macro.BUNDLE_HALL,
            // dir : "texture",

            // normalSprite:{urls : ["lobby_texture"] , key : "update_status_hot"},
            // pressedSprite:{urls : ["lobby_texture"] , key : "update_status_fav"},
            // hoverSprite:{urls : ["lobby_texture"] , key : "update_status_new"},
            // bundle : Macro.BUNDLE_RESOURCES,
            // dir : "common/images",

            view: this,
            complete: (type, spriteFrame) => {
                if (type == ButtonSpriteType.Norml) {
                    button.setContentSize(spriteFrame.getOriginalSize());
                    btn.target.setContentSize(spriteFrame.getOriginalSize());
                }
            },
        })
    }

    private onLoadParticle() {
        let name = "onLoadParticle";
        if (this.content.getChildByName(name)) {
            return;
        }
        this.content.removeAllChildren();
        let node = new cc.Node();
        node.name = name;
        this.content.addChild(node);
        let sys = node.addComponent(cc.ParticleSystem);
        sys.loadFile({
            //不以加载资源目录形式加载
            url: "particle/test",

            //以加载资源目录方式加载
            // url : "test",
            // dir : "particle",

            view: this,
        })
    }

    private onLoadSpine() {
        let name = "onLoadSpine";
        if (this.content.getChildByName(name)) {
            return;
        }

        this.content.removeAllChildren();
        let node = new cc.Node();
        node.name = name;
        this.content.addChild(node);
        let spine = node.addComponent(sp.Skeleton);
        spine.loadSkeleton({
            view: this,
            complete: () => {
                spine.setAnimation(0, "walk", true);
            },

            //不提前以目录形式加载
            url: "spine/raptor",

            //提示以目录形式加载资源
            // url : "raptor",
            // dir : "spine",

        })
        node.y = - this.content.height / 2;
        node.scale = 0.5;
    }

    private onLoadNetSpine() {
        let name = "onLoadNetSpine";
        if (this.content.getChildByName(name)) {
            return;
        }
        this.content.removeAllChildren();
        let node = new cc.Node();
        node.name = name;
        this.content.addChild(node);
        let spine = node.addComponent(sp.Skeleton);
        spine.loadRemoteSkeleton({
            view: this,
            path: "http://172.20.11.130",
            name: "raptor",
            complete: (data: sp.SkeletonData) => {
                if (data) {
                    spine.setAnimation(0, "walk", true);
                }
            }
        })
        node.y = -this.content.height / 2;
        node.scale = 0.7;
    }

    private onLoadDir() {
        let name = "onLoadDir";
        if (this.content.getChildByName(name)) {
            return;
        }
        this.content.removeAllChildren();
        let node = new cc.Node();
        node.name = name;
        this.content.addChild(node);
        //添加显示渲染节点
        node.addComponent(cc.Sprite);
        //添加动画
        let ani = node.addComponent(cc.Animation);

        let makeAnimation = (data: cc.Asset | cc.Asset[]) => {
            if (data) {
                let arr: cc.SpriteFrame[] = (<cc.SpriteFrame[]>data);
                let clip = cc.AnimationClip.createWithSpriteFrames(arr, arr.length);
                clip.name = "run";
                clip.wrapMode = cc.WrapMode.Loop;
                ani.addClip(clip);
                ani.play("run");
            }
        }

        loadDirRes({
            //未提前以目录资源进行加载
            url:"texture/sheep",

            //提前以目录资源进行加载过资源
            // url : "",
            // dir : "texture/sheep",

            type:cc.SpriteFrame,
            view : this,
            onComplete:(data)=>{
                makeAnimation(data.data);
            }
        })


    }

    private onLoadDragon() {
        let name = "onLoadDragon";
        if (this.content.getChildByName(name)) {
            return;
        }
        this.content.removeAllChildren();
        let node = new cc.Node();
        node.name = name;
        this.content.addChild(node);
        //添加动画
        let ani = node.addComponent(dragonBones.ArmatureDisplay);
        ani.loadDisplay({
            // 未提前加载目录资源方式
            assetUrl: "dragonBones/NewDragonTest",
            atlasUrl: "dragonBones/texture",

            // 提前加载目录资源方式
            // assetUrl: "NewDragonTest",
            // atlasUrl: "texture",
            // dir : "dragonBones",

            view: this,
            complete: (asset, atlas) => {
                if (asset && atlas) {
                    ani.armatureName = "armatureName";
                    ani.setAnimationCacheMode(dragonBones.ArmatureDisplay.AnimationCacheMode.REALTIME);
                    ani.timeScale = 1;
                    ani.playTimes = 0;
                    ani.playAnimation("stand", 0);
                }
            }
        })
        node.y = -this.content.height / 2;
        node.scale = 0.9;
    }

    private onTableView() {
        let name = "testTableView";
        if (this.content.getChildByName(name)) {
            return;
        }
        this.content.removeAllChildren();
        let node = cc.instantiate(this.tableView);
        this.content.addChild(node);
        node.setPosition(cc.v2(0, 0));
    }
}
